package com.lagou.module3hw.hw3;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 统计字符串"123,456,789,123,456"中每个数字字符串出现的次数并打印出来
 */
public class HashMapTest {

    public static void main(String[] args) {


        String str = "123,456,789,123,456";
        HashMap<String,Integer> hashMap = new HashMap<>();
        String[] split = str.split(",");

        //遍历分割后的字符串数组，把每个字符串作为key，出现的个数为value放入hashMap中
        for(String s : split){
            if(hashMap.containsKey(s)){
                Integer it = hashMap.get(s) + 1;
                hashMap.put(s,it);
            }else {
                hashMap.put(s, 1);
            }
        }

        //遍历hashMap集合
        Set<Map.Entry<String, Integer>> entries = hashMap.entrySet();
        for (Map.Entry<String, Integer> map :entries) {
            System.out.println(map.getKey() + "出现了" + map.getValue() + "次");
        }
    }
}
